package com.pai4j.user.repository.dao;

import com.pai4j.user.repository.entity.PendingApprovalEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 统一待审核DAO
 */
public interface IPendingApprovalDAO extends JpaRepository<PendingApprovalEntity, Long> {

    /**
     * 查询待审核列表（所有状态，含已读未读）
     * 
     * @param approverAccount 审批人账号
     * @param approvalType 审批类型，null则查询所有类型
     * @param approvalStatus 审批状态
     * @return 待审核列表，按创建时间倒序
     */
    @Query("SELECT p FROM PendingApprovalEntity p " +
           "WHERE p.approverAccount = :approverAccount " +
           "AND (:approvalType IS NULL OR p.approvalType = :approvalType) " +
           "AND p.approvalStatus = :approvalStatus " +
           "ORDER BY p.createDate DESC")
    List<PendingApprovalEntity> findPendingApprovals(
            @Param("approverAccount") String approverAccount,
            @Param("approvalType") Integer approvalType,
            @Param("approvalStatus") Integer approvalStatus);

    /**
     * 查询未读待审核列表
     * 
     * @param approverAccount 审批人账号
     * @param approvalType 审批类型，null则查询所有类型
     * @param approvalStatus 审批状态
     * @return 未读待审核列表，按创建时间倒序
     */
    @Query("SELECT p FROM PendingApprovalEntity p " +
           "WHERE p.approverAccount = :approverAccount " +
           "AND (:approvalType IS NULL OR p.approvalType = :approvalType) " +
           "AND p.approvalStatus = :approvalStatus " +
           "AND p.isRead = false " +
           "ORDER BY p.createDate DESC")
    List<PendingApprovalEntity> findUnreadPendingApprovals(
            @Param("approverAccount") String approverAccount,
            @Param("approvalType") Integer approvalType,
            @Param("approvalStatus") Integer approvalStatus);

    /**
     * 统计未读待审核数量
     * 
     * @param approverAccount 审批人账号
     * @param approvalType 审批类型，null则统计所有类型
     * @param approvalStatus 审批状态
     * @return 未读数量
     */
    @Query("SELECT COUNT(p) FROM PendingApprovalEntity p " +
           "WHERE p.approverAccount = :approverAccount " +
           "AND (:approvalType IS NULL OR p.approvalType = :approvalType) " +
           "AND p.approvalStatus = :approvalStatus " +
           "AND p.isRead = false")
    Long countUnreadPendingApprovals(
            @Param("approverAccount") String approverAccount,
            @Param("approvalType") Integer approvalType,
            @Param("approvalStatus") Integer approvalStatus);

    /**
     * 标记待审核为已读（指定类型）
     * 
     * @param approverAccount 审批人账号
     * @param approvalType 审批类型
     * @param approvalStatus 审批状态
     * @return 标记数量
     */
    @Modifying
    @Query("UPDATE PendingApprovalEntity p SET p.isRead = true, p.updateDate = CURRENT_TIMESTAMP " +
           "WHERE p.approverAccount = :approverAccount " +
           "AND p.approvalType = :approvalType " +
           "AND p.approvalStatus = :approvalStatus " +
           "AND p.isRead = false")
    int markApprovalsAsReadByType(
            @Param("approverAccount") String approverAccount,
            @Param("approvalType") Integer approvalType,
            @Param("approvalStatus") Integer approvalStatus);

    /**
     * 标记所有待审核为已读（所有类型）
     * 
     * @param approverAccount 审批人账号
     * @param approvalStatus 审批状态
     * @return 标记数量
     */
    @Modifying
    @Query("UPDATE PendingApprovalEntity p SET p.isRead = true, p.updateDate = CURRENT_TIMESTAMP " +
           "WHERE p.approverAccount = :approverAccount " +
           "AND p.approvalStatus = :approvalStatus " +
           "AND p.isRead = false")
    int markAllApprovalsAsRead(
            @Param("approverAccount") String approverAccount,
            @Param("approvalStatus") Integer approvalStatus);

    /**
     * 根据业务记录ID和类型查询待审核记录
     * 用于业务表更新时同步更新待审核表
     * 
     * @param approvalType 审批类型
     * @param bizRecordId 业务记录ID
     * @return 待审核记录
     */
    @Query("SELECT p FROM PendingApprovalEntity p " +
           "WHERE p.approvalType = :approvalType " +
           "AND p.bizRecordId = :bizRecordId")
    PendingApprovalEntity findByTypeAndBizRecordId(
            @Param("approvalType") Integer approvalType,
            @Param("bizRecordId") Long bizRecordId);

    /**
     * 更新审批状态（同步业务表状态变更）
     * 
     * @param approvalType 审批类型
     * @param bizRecordId 业务记录ID
     * @param approvalStatus 新的审批状态
     * @param handledByAccount 处理人账号
     * @param approvalRemark 审批意见
     * @return 更新数量
     */
    @Modifying
    @Query("UPDATE PendingApprovalEntity p " +
           "SET p.approvalStatus = :approvalStatus, " +
           "p.handledByAccount = :handledByAccount, " +
           "p.approvalTime = CURRENT_TIMESTAMP, " +
           "p.approvalRemark = :approvalRemark, " +
           "p.updateDate = CURRENT_TIMESTAMP " +
           "WHERE p.approvalType = :approvalType " +
           "AND p.bizRecordId = :bizRecordId")
    int updateApprovalStatus(
            @Param("approvalType") Integer approvalType,
            @Param("bizRecordId") Long bizRecordId,
            @Param("approvalStatus") Integer approvalStatus,
            @Param("handledByAccount") String handledByAccount,
            @Param("approvalRemark") String approvalRemark);
}
